﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ConyifutypeController : Controller
    {
        private ConyifutypeInfo yaxinfo = new ConyifutypeInfo();
        public ActionResult ConyifutypeIndex()
        {
            return View();
        }

        public ActionResult GetList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string param = pvm.query;
                query = " and Yifumc like '%" + param + "%'";
            }
            IList<ConyifutypeInfo> yaxlist = yaxinfo.GetList(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = yaxinfo.GetCount(query);
            List<string> v;
            foreach (ConyifutypeInfo item in yaxlist)
            {
                #region
                v = new List<string>();
                v.Add(item.Yifu_ID.ToString());
                v.Add(item.Yifumc);
                v.Add(item.Count_xiyi.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.Yifu_ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        public ActionResult DeleteConyifutype(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = yaxinfo.DeleteConyifutype(strId);
            return Json(result);
        }

        public ActionResult AddConyifutype()
        {
            return View();
        }

        public ActionResult SaveAdd(ConyifutypeVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                ConyifutypeInfo yax = new ConyifutypeInfo()
                {
                    Yifumc = model.Yifumc,
                    Count_xiyi = model.Count_xiyi
                };
                result = yaxinfo.SaveAdd(yax);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditConyifutype(string id)
        {
            ConyifutypeVM vm = new ConyifutypeVM();
            ConyifutypeInfo info = new ConyifutypeInfo();
            yaxinfo = info.GetInfoOne(id);
            vm.Yifu_ID = yaxinfo.Yifu_ID;
            vm.Yifumc = yaxinfo.Yifumc;
            vm.Count_xiyi = yaxinfo.Count_xiyi;
            return View(vm);
        }

        public ActionResult SaveEdit(ConyifutypeVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                ConyifutypeInfo yax = new ConyifutypeInfo()
                {
                    Yifu_ID = model.Yifu_ID,
                    Yifumc = model.Yifumc,
                    Count_xiyi = model.Count_xiyi
                };
                result = yaxinfo.SaveEdit(yax);
            }
            return Json(result);
        }
    }
}
